import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';

interface Message {
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	params: {
		student_no: string;
		productid: string;
		faceid: string;
		sucfaceids: string;
	};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	const q = query(action_id, session_id, systemid);
	q.prepare('face_gallery', ['_id', 'sucfaceids', 'faceid'], { student_no: message.params.student_no, productid: message.params.productid }, 1, 1, [], []);
	const [d] = await q.exec();
	if (!d.length) {
		return { data: { code: 0, msg: `表face_gallery中不存在学生${message.params.student_no}` } };
	}
	const s = d[0] as { _id: string; sucfaceids: string; faceid: string; };
	const { update, exec } = operate(action_id, session_id, systemid);
	update('face_gallery', { _id: s._id }, { sucfaceids: message.params.sucfaceids, faceid: message.params.faceid });
	const r = await exec();
	return {
		data: { ...r, sucfaceids: s.sucfaceids, faceid: s.faceid }
	};
}
